package cn.kgc.vue.controller;

import cn.kgc.vue.commons.QueryCondition;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.entity.Score;
import cn.kgc.vue.service.ScoreService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

/**
 * @author 课工场
 * @date 2024/4/18
 * @description
 */
@RestController
@RequestMapping("score")
@Api(tags = "成绩模块")
public class ScoreController {


    @Resource
    private ScoreService scoreService;

    @PostMapping("page")
    @ApiOperation("成绩分页数据")
    public ResponseResult scorePage(@RequestBody QueryCondition<Score> queryCondition){

        return scoreService.scorePage(queryCondition);
    }


    @PostMapping("addOrUpdate")
    @ApiOperation("更新添加成绩")
    public ResponseResult addOrUpdateScore(@RequestBody Score score){

        Date examTime = score.getExamTime();
        String studentNo = score.getStudentNo();

        // 判定学生的考试时间是否重复
        Integer id = score.getId();

        if (ObjectUtils.isNull(id)){
            LambdaQueryWrapper<Score> lambda = new QueryWrapper<Score>().lambda();
            lambda.eq(Score::getExamTime,examTime)
                    .eq(Score::getStudentNo,studentNo);
            Score one = scoreService.getOne(lambda);
            if (ObjectUtils.isNotNull(one)){
                return ResponseResult.fail().message("在同一个时间不能添加两次考试");
            }
        }

        scoreService.saveOrUpdate(score);

        return ResponseResult.success();
    }



    @GetMapping("getById/{id}")
    @ApiOperation("根据id获取成绩")
    public ResponseResult getScoreById(@PathVariable("id") Integer id){

        Score score = scoreService.getById(id);

        return ResponseResult.success()
                .data("score",score);
    }


    @DeleteMapping("delete/{id}")
    @ApiOperation("根据id获取成绩")
    public ResponseResult deleteScoreById(@PathVariable("id") Integer id){


        scoreService.removeById(id);
        return ResponseResult.success()
                .message("删除成功");
    }

    @PostMapping("import")
    @ApiOperation("批量导入")
    public ResponseResult importScore(MultipartFile file) throws Exception {

        return scoreService.importScore(file);
    }


    @PostMapping("export")
    @ApiOperation("批量导出")
    public void exportScore(HttpServletResponse response, @RequestBody Map<String,Object> exportParams) throws Exception {

       scoreService.exportScore(response,exportParams);
    }



    @GetMapping("dataVisual")
    @ApiOperation("数据可视化")
    public ResponseResult scoreDataVisual(){

       return scoreService.scoreDataVisual();

    }


}
